// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.butler.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.butler.controller.request.ImportQuestionAnswerReq;
import com.liuxinlong.modules.butler.controller.request.QueryFaqReq;
import com.liuxinlong.modules.butler.controller.request.UpdateQuestionAnswerReq;
import com.liuxinlong.modules.butler.service.FaqService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.QuestionAnswer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * FAQ控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/faq")
public class FaqController {

    @Autowired
    private FaqService faqService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询信息
     *
     * @param req 请求信息
     * @return FAQ信息列表
     */
    @PostMapping("page")
    public BaseResp pageQuestionAnswer(@RequestBody QueryFaqReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("keyword", req.getKeyword());
        queryMap.put("subType", req.getSubType());
        List<Map<String, Object>> dataList = faqService.pageQuestionAnswer(queryMap);
        int totalNum = faqService.getQuestionAnswerCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询FAQ信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增FAQ信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增FAQ信息", business = LogBusinessConstants.QUESTION_ANSWER, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addQuestionAnswer(@RequestBody UpdateQuestionAnswerReq req) {
        QuestionAnswer questionAnswer = new QuestionAnswer();
        questionAnswer.setQuestion(req.getQuestion());
        questionAnswer.setAnswer(req.getAnswer());
        questionAnswer.setSubType(req.getSubType());
        questionAnswer.setShowIndex(req.getShowIndex());
        questionAnswer.setTrain(req.getTrain());
        questionAnswer.setAttach(req.getAttach());
        faqService.addQuestionAnswer(questionAnswer,req.getForceFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增FAQ信息成功");
        return resp;
    }

    /**
     * 修改FAQ信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改FAQ信息", business = LogBusinessConstants.QUESTION_ANSWER, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateQuestionAnswer(@RequestBody UpdateQuestionAnswerReq req) {
        QuestionAnswer questionAnswer = new QuestionAnswer();
        questionAnswer.setId(req.getId());
        questionAnswer.setQuestion(req.getQuestion());
        questionAnswer.setAnswer(req.getAnswer());
        questionAnswer.setSubType(req.getSubType());
        questionAnswer.setShowIndex(req.getShowIndex());
        questionAnswer.setTrain(req.getTrain());
        questionAnswer.setAttach(req.getAttach());
        faqService.updateQuestionAnswer(questionAnswer,req.getForceFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改FAQ信息成功");
        return resp;
    }

    /**
     * 删除FAQ信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除FAQ信息", business = LogBusinessConstants.QUESTION_ANSWER, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteQuestionAnswer(@RequestBody BaseDeleteReq req) {
        faqService.deleteQuestionAnswer(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除FAQ信息成功");
        return resp;
    }

    /**
     * 导出FAQ信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出FAQ信息", business = LogBusinessConstants.QUESTION_ANSWER,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportQuestionAnswer(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        faqService.exportQuestionAnswer(req.getId(), response);
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入FAQ数据", business = LogBusinessConstants.QUESTION_ANSWER,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importDetails(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = faqService.importQuestionAnswers((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入FAQ信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入FAQ数据", business = LogBusinessConstants.QUESTION_ANSWER,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddQuestionAnswer(@RequestBody ImportQuestionAnswerReq req) {
        return faqService.batchAddQuestionAnswer(req.getDataList());
    }

    /**
     * 记录点击次数
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("count")
    @LogAnnotation(value = "记录点击次数", business = LogBusinessConstants.QUESTION_ANSWER, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp countClick(@RequestBody BaseDeleteReq req) {
        faqService.countClick(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("记录点击次数信息成功");
        return resp;
    }

}
